package com.dgut.service.serviceImpl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dgut.common.NoticeInfo;
import com.dgut.common.NoticeType;
import com.dgut.common.StateForOrder;
import com.dgut.common.StateOfNotice;
import com.dgut.mapper.LegalRecourseMapper;
import com.dgut.mapper.NoticeMapper;
import com.dgut.pojo.LegalRecourse;
import com.dgut.pojo.Notice;
import com.dgut.pojo.OrderFlag;
import com.dgut.service.NoticeService;
import com.dgut.utils.PageRequestInfo;
import com.dgut.utils.PageResult;
import com.dgut.utils.PageUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

@Service
public class NoticeServiceImpl implements NoticeService {
    @Resource
    private NoticeMapper noticeMapper;
    @Resource
    private LegalRecourseMapper legalRecourseMapper;


    /**
     * start-zwb
     */
    /**
     * end-zwb
     */


    /**
     * start-gxd
     */
    /**
     * end-gxd
     */


    /**
     * start-lzh
     */
    /**
     * end-lzh
     */





    /**
     * start-hwf
     */

    /**
     * 根据id获取未读信息数量
     * @param id
     * @param state
     * @return
     */
    @Override
    public int selectNoticeCount(int id,String state) {
        return noticeMapper.selectNoticeCount(id,state);
    }

    /**
     * 根据id,state,pageRequestInfo获取消息分页
     * @param id 接收者id
     * @param state 消息状态
     * @param pageRequestInfo 分页信息
     * @return
     */
    @Override
    public PageResult getNoticePage(int id, String state, PageRequestInfo pageRequestInfo) {
        Page<Notice> page = new Page<>(pageRequestInfo.getPageNum(),pageRequestInfo.getPageSize());
        Page<Notice> pageResult = noticeMapper.selectPage(page, Wrappers.<Notice>lambdaQuery().eq(Notice::getReceiverId,id).eq(Notice::getState,state));
        return PageUtils.getPageResult(pageResult);
    }

    @Override
    public boolean readNotice(int id,int userId,String state) {
        if(noticeMapper.readNotice(id,userId,state) >= 1){
            return true;
        }else{
            return false;
        }
    }


    /**
     * 订单状态改变通知
     * @param orderFlag 订单
     */
    @Override
    public void orderChangedStateNotice(OrderFlag orderFlag) {
        String noticeContent = "";
        noticeContent = "订单"+orderFlag.getId()+":"+ NoticeInfo.ORDER_CHANGED_STATE_INFO+orderFlag.getState();
        Notice notice = new Notice();
        notice.setState(StateOfNotice.UNREAD);
        notice.setContext(noticeContent);
        notice.setTime(new Date());
        notice.setType(NoticeType.ORDER_CHANGED_STATE);
        notice.setReceiverId(orderFlag.getBuyerId());
        noticeMapper.insert(notice);
        notice.setReceiverId(orderFlag.getServiceId());
    }

    /**
     *抢单结果通知
     * @param legalRecourseId 法律求助id
     * @param serviceProviderId 抢单成功服务商id
     */
    @Override
    public void resultOfOrderGrabNotice(int legalRecourseId, int serviceProviderId) {

        /**
         * 为失败者发同通知
         */
        String noticeContent = "法律求助"+legalRecourseId+":"+NoticeInfo.FALSE_RESULT_OF_ORDER_GRAB_INFO;
        int[] ids = noticeMapper.getServiceProviderIds(legalRecourseId,serviceProviderId);
        Notice notice = new Notice();
        for (int id:ids
             ) {
            notice.setReceiverId(id);
            notice.setContext(noticeContent);
            notice.setTime(new Date());
            notice.setState(StateOfNotice.UNREAD);
            notice.setType(NoticeType.RESULT_OF_ORDER_GRAB);
            noticeMapper.insert(notice);
        }

        /**
         * 为抢单成功者发通知
         */
        noticeContent = "法律求助"+legalRecourseId+":"+NoticeInfo.SUCCESS_ORDER_GRAB_INFO;
        notice.setReceiverId(serviceProviderId);
        notice.setContext(noticeContent);
        notice.setTime(new Date());
        notice.setState(StateOfNotice.UNREAD);
        notice.setType(NoticeType.RESULT_OF_ORDER_GRAB);
        noticeMapper.insert(notice);
    }

    /**
     * 抢单通知
     * @param buyerId 采买方id 接收通知
     * @param legalRecourseId 法律求助id
     * @param serviceProviderId 抢单服务商id
     */
    @Override
    public void orderRequestNotice(int buyerId,int legalRecourseId,int serviceProviderId) {
        String noticeContent = "法律求助"+legalRecourseId+":"+serviceProviderId+NoticeInfo.ORDER_REQUEST_INFO;
        Notice notice = new Notice();
        notice.setReceiverId(buyerId);
        notice.setContext(noticeContent);
        notice.setTime(new Date());
        notice.setState(StateOfNotice.UNREAD);
        notice.setType(NoticeType.ORDER_REQUEST);
        noticeMapper.insert(notice);
    }

    /**
     *订单完成通知
     * @param orderId 订单id
     * @param receiver 接收者id(服务商/采买方)
     */
    @Override
    public void orderFinishedNotice(int orderId,int receiver) {
        String noticeContent = "订单"+orderId+":"+NoticeInfo.ORDER_FINISHED_INFO;
        Notice notice = new Notice();
        notice.setReceiverId(receiver);
        notice.setContext(noticeContent);
        notice.setTime(new Date());
        notice.setState(StateOfNotice.UNREAD);
        notice.setType(NoticeType.ORDER_FINISHED);
        noticeMapper.insert(notice);
    }

    /**
     * 取消订单通知
     * @param orderId 订单id
     * @param receiver 接收者id(服务商)
     */
    @Override
    public void cancelOrderRequestNotice(int orderId,int receiver) {
        String noticeContent = "订单"+orderId+":"+NoticeInfo.CANCEL_ORDER_REQUEST_INFO;
        Notice notice = new Notice();
        notice.setReceiverId(receiver);
        notice.setContext(noticeContent);
        notice.setTime(new Date());
        notice.setState(StateOfNotice.UNREAD);
        notice.setType(NoticeType.CANCEL_ORDER_REQUEST);
        noticeMapper.insert(notice);
    }

    /**
     * 取消订单处理结果通知
     * @param orderId 订单id
     * @param receiver 接收者id(采买方)
     */
    @Override
    public void cancelOrderResultNotice(int orderId, int receiver) {
        String noticeContent = "订单"+orderId+":"+NoticeInfo.CANCEL_ORDER_RESULT_INFO;
        Notice notice = new Notice();
        notice.setReceiverId(receiver);
        notice.setContext(noticeContent);
        notice.setTime(new Date());
        notice.setState(StateOfNotice.UNREAD);
        notice.setType(NoticeType.CANCEL_ORDER_RESULT);
        noticeMapper.insert(notice);
    }

    @Override
    public void assignOrder(int orderId, int receiver) {
        String noticeContent = "订单"+orderId+":"+NoticeInfo.ASSIGN_ORDER;
        Notice notice = new Notice();
        notice.setReceiverId(receiver);
        notice.setContext(noticeContent);
        notice.setTime(new Date());
        notice.setState(StateOfNotice.UNREAD);
        notice.setType(NoticeType.ASSIGN_ORDER);
        noticeMapper.insert(notice);
    }

    @Override
    public void resultProcessVoucher(int orderId,int receiver,boolean result) {
        String noticeContent = "订单"+orderId+":";
        if(result){
            noticeContent+=NoticeInfo.RESULT_PROCESS_VOUCHER_SUCCESS;
        }else{
            noticeContent+=NoticeInfo.RESULT_PROCESS_VOUCHER_FAIL;
        }
        Notice notice = new Notice();
        notice.setReceiverId(receiver);
        notice.setContext(noticeContent);
        notice.setTime(new Date());
        notice.setState(StateOfNotice.UNREAD);
        notice.setType(NoticeType.ASSIGN_ORDER);
        noticeMapper.insert(notice);
    }

    /**
     * end-hwf
     */
}
